package pw.goyd.game.api;

import pw.goyd.game.constant.setting.GameSetting;
import pw.goyd.game.manager.RoomManager;
import pw.goyd.game.model.Room;
import pw.goyd.game.server.Log;
import pw.goyd.subscribe.PlayerCustomer;

import java.util.Collections;
import java.util.List;

/**
 * 金币计算类
 *
 * @author StanWind
 */
public class CalcGolds {
    /**
     * 会自动排序然后计算
     *
     * @param orderedList 玩家List
     */
    public static void settleAccounts(List<PlayerCustomer> orderedList) {
        Collections.sort(orderedList);  // 按照牌面排序
        int index = -1;
        // 找到玩家所在房间
        Room r = RoomManager.getRoom(orderedList.get(0).getRoomID());
        if (r == null)
            return;
        PlayerCustomer declarer = r.getDeclarerPlayer();
        if (declarer == null)
            return;
        String acc_id = declarer.getAccount_id();

        // 找出在排序过后的玩家列中庄家的位置
        for (int i = 0; i < orderedList.size(); i++) {
            if (orderedList.get(i).getAccount_id().equals(acc_id)) {
                index = i;
                break;
            }
        }
        int money = orderedList.get(index).getDeclarer() * r.getLeastSocre() * GameSetting.GLOBAL_MONEY; // 基础的分数 全局五倍
        Log.getLogger().info("结算---->基础 " + money + " 全局:" + GameSetting.GLOBAL_MONEY + " 庄家:" + orderedList.get(index).getDeclarer() + "底分:" + r.getLeastSocre());
        int cache = money;
        // 庄家获得该下标之前的所有下注金钱
        for (int i = 0; i < index; i++) {
            money = setMultiple(money, orderedList.get(index)); // 根据庄家的牌型判断是否翻倍
            money *= orderedList.get(i).getBet();               // 根据玩家下注数量翻倍
            orderedList.get(i).setChangingGolds(money * -1);
            orderedList.get(index).setChangingGolds(orderedList.get(index).getChangingGolds() + money);
            Log.getLogger().info("结算---->庄家 赢得 " + money + "金币");
            money = cache;
        }
        for (int i = index + 1; i < orderedList.size(); i++) {
            money = setMultiple(money, orderedList.get(i)); // 根据玩家的牌型判断是否翻倍
            money *= orderedList.get(i).getBet();           // 根据玩家下注数量翻倍
            orderedList.get(index).setChangingGolds(orderedList.get(index).getChangingGolds() + money * -1);
            orderedList.get(i).setChangingGolds(money);
            Log.getLogger().info("结算---->庄家 输掉 " + money + "金币");
            money = cache;
        }
    }

    /**
     * 根据牛设置金钱翻倍的数量
     *
     * @param money  提交的钱
     * @param player 玩家
     * @return 翻倍后的钱
     */
    public static int setMultiple(int money, PlayerCustomer player) {
        int m = money;
        switch (player.getResType()) {
            // 牛7-9翻两倍
            case Seven:
            case Eight:
            case Nine:
                m = money * 2;
                break;
            // 牛牛3倍
            case Ten:
                m = money * 3;
                break;
            // 四炸4倍
            case Bomb:
                m = money * 4;
                break;
            // 五花牛5倍
            case Streaky:
                m = money * 5;
                break;
            // 五小牛8倍
            case Small:
                m = money * 8;
                break;
            default:
                // 不变
                break;
        }
        return m;
    }
}
